package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.PublishedGoodsItem;
import com.shaoshaohuo.app.net.HttpRequest;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.PublishManagerActivity;
import com.shaoshaohuo.app.ui.ec.BuysGoodsActivity;
import com.shaoshaohuo.app.ui.shipper.SendGoodsActivity;
import com.shaoshaohuo.app.utils.AnimationUtil;
import com.shaoshaohuo.app.utils.AreaUtil;
import com.shaoshaohuo.app.utils.StringHelper;
import com.shaoshaohuo.app.utils.StringUtil;
import com.shaoshaohuo.app.utils.ToastUtil;

import java.util.List;

/**
 * Created by YinSF on 16/1/19.
 */
public class PublishedGoodsListAdapter <T> extends MyBaseAdapter<PublishedGoodsItem> implements View.OnClickListener {
    public PublishedGoodsListAdapter(Context ctx, List<PublishedGoodsItem> list, boolean flag) {
        super(ctx, list, flag);
    }

    @Override
    public View getView(int pos, View view, ViewGroup group) {
        if (view == null) {
            view = View.inflate(context, R.layout.item_goods_published, null);
        }
        GoodsViewHolder goodsHolder = GoodsViewHolder.getHolder(view);
        PublishedGoodsItem goods = list.get(pos);

        goodsHolder.tv_name.setText(goods.getOrdername());
        if ("0.00".equals(goods.getShippweight())) {
            goodsHolder.tv_num.setText(goods.getShipparea() + "立方米");
        } else {
            goodsHolder.tv_num.setText(goods.getShippweight() + "千克");
        }
        goodsHolder.tv_start_time.setText("发货：" + goods.getOrigintime());
        goodsHolder.tv_start_ad.setText(AreaUtil.getAreaFullname(goods.getOrigincityid()) + goods.getOriginaddress());
        goodsHolder.tv_end_ad.setText(AreaUtil.getAreaFullname(goods.getReceivecityid()) + goods.getReceiveaddress());
        goodsHolder.tv_notice_count.setText(goods.getQiangCount() + "人");
        goodsHolder.updateTime.setText(StringHelper.formatDateMinute(goods.getUdate()));
        String bCount = StringUtil.isEmpty(goods.getBnum()) ? "1" : goods.getBnum();
        goodsHolder.browseCount.setText("浏览量：" + bCount + "次");

        goodsHolder.deleteLayout.setTag(goods.getId());
        goodsHolder.editLayout.setTag(goods.getOrderid());
        goodsHolder.freshLayout.setTag(goods.getId());
        goodsHolder.freshLayout.setTag(R.id.textview_cdate, goodsHolder.updateTime);

        goodsHolder.deleteLayout.setOnClickListener(this);
        goodsHolder.editLayout.setOnClickListener(this);
        goodsHolder.freshLayout.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        switch (viewId) {
            case R.id.layout_fresh: {
                String goodsId = (String) v.getTag();
                TextView updateTime = (TextView) v.getTag(R.id.textview_cdate);
                requestDataFresh(goodsId, updateTime);
            }
                break;
            case R.id.layout_edit: {
                String goodsId = (String) v.getTag();
                Intent intent = new Intent(context, SendGoodsActivity.class);
                intent.putExtra(BuysGoodsActivity.EXTRA_RECORD_ID, goodsId);
                context.startActivity(intent);
            }
                break;
            case R.id.layout_delete: {
                String goodsId = (String) v.getTag();
                requestDataDelete(goodsId);
            }
                break;
            default:
                break;
        }
    }

    private void requestDataFresh(String id, final TextView updateTime) {

        RequestService.getInstance().publishedGoodsListRefresh(context, id, BaseEntity.class, new HttpRequest.RequestListener() {

            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                if (resultData.isOk()) {
                    updateTime.setText("发布日期：" + StringHelper.formatDateMinute("" + System.currentTimeMillis()));
                    AnimationUtil.emphasize(context, updateTime, 0);
                    ToastUtil.toast("刷新置顶成功");
                } else {
                    ToastUtil.toast(resultData.getMsg());
                }
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                ToastUtil.toast(msg);
            }
        });
    }

    private void requestDataDelete(String id) {

        RequestService.getInstance().publishedGoodsListDelete(context, id, BaseEntity.class, new HttpRequest.RequestListener() {

            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                if (resultData.isOk()) {
                    ToastUtil.toast("删除成功");
                    ((PublishManagerActivity) context).onRefresh();
                } else {
                    ToastUtil.toast(resultData.getMsg());
                }
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                ToastUtil.toast(msg);
            }
        });
    }
    static class GoodsViewHolder {

        TextView tv_long;
        TextView tv_notice_count;
        TextView tv_end_ad;
        TextView tv_start_ad;
        TextView tv_start_time;
        TextView tv_name;
        TextView tv_num;
        TextView updateTime;
        TextView browseCount;
        LinearLayout freshLayout;
        LinearLayout editLayout;
        LinearLayout deleteLayout;

        public GoodsViewHolder(View convertView) {
            tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            tv_num = (TextView) convertView.findViewById(R.id.tv_num);
            tv_start_time = (TextView) convertView.findViewById(R.id.tv_start_time);
            tv_start_ad = (TextView) convertView.findViewById(R.id.tv_start_ad);
            tv_end_ad = (TextView) convertView.findViewById(R.id.tv_end_ad);
            tv_notice_count = (TextView) convertView.findViewById(R.id.tv_notice_count);
            tv_long = (TextView) convertView.findViewById(R.id.tv_long);
            freshLayout = (LinearLayout) convertView.findViewById(R.id.layout_fresh);
            editLayout = (LinearLayout) convertView.findViewById(R.id.layout_edit);
            deleteLayout = (LinearLayout) convertView.findViewById(R.id.layout_delete);
            updateTime = (TextView) convertView.findViewById(R.id.textview_cdate);
            browseCount = (TextView) convertView.findViewById(R.id.textview_browse_count);

            convertView.setTag(this);
        }

        public static GoodsViewHolder getHolder(View convertView) {
            return (GoodsViewHolder) (convertView.getTag() != null ? convertView.getTag() : new GoodsViewHolder(convertView));
        }
    }
}
